'use strict'

const {
  quoteSchema,
  findQuotes,
  findQuoteById,
  insertQuote,
  likeQuote,
  deleteQuote
} = require('../models/quote')

module.exports = async function (fastify, opts) {
  // Define JSON schema for route validation
  const getQuotesSchema = {
    schema: {
      querystring: {
        type: 'object',
        properties: {
          category: { type: 'string' },
          limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
          skip: { type: 'integer', minimum: 0, default: 0 }
        }
      },
      response: {
        200: {
          type: 'array',
          items: quoteSchema
        }
      }
    }
  }

  const getQuoteSchema = {
    schema: {
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
        },
        required: ['id']
      },
      response: {
        200: quoteSchema,
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  const createQuoteSchema = {
    schema: {
      body: {
        type: 'object',
        properties: {
          text: { type: 'string' },
          author: { type: 'string' },
          category: { type: 'string' }
        },
        required: ['text', 'author', 'category']
      },
      response: {
        201: quoteSchema
      }
    }
  }

  const likeQuoteSchema = {
    schema: {
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
        },
        required: ['id']
      },
      response: {
        200: quoteSchema,
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  const deleteQuoteSchema = {
    schema: {
      params: {
        type: 'object',
        properties: {
          id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' }
        },
        required: ['id']
      },
      response: {
        200: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        },
        404: {
          type: 'object',
          properties: {
            message: { type: 'string' }
          }
        }
      }
    }
  }

  // Routes
  fastify.get('/api/quotes', getQuotesSchema, async function (request, reply) {
    const { category, limit, skip } = request.query
    const query = category ? { category } : {}
    const quotes = await findQuotes(fastify, query, limit, skip)
    return quotes
  })

  fastify.get('/api/quotes/random', async function (request, reply) {
    const quotes = await findQuotes(fastify, {})
    if (quotes.length === 0) {
      return reply.code(404).send({ message: 'No quotes found' })
    }
    const randomIndex = Math.floor(Math.random() * quotes.length)
    return quotes[randomIndex]
  })

  fastify.get('/api/quotes/:id', getQuoteSchema, async function (request, reply) {
    const { id } = request.params
    const quote = await findQuoteById(fastify, id)
    if (!quote) {
      return reply.code(404).send({ message: 'Quote not found' })
    }
    return quote
  })

  fastify.post('/api/quotes', createQuoteSchema, async function (request, reply) {
    const newQuote = await insertQuote(fastify, request.body)
    reply.code(201)
    return newQuote
  })

  fastify.post('/api/quotes/:id/like', likeQuoteSchema, async function (request, reply) {
    const { id } = request.params
    const quote = await likeQuote(fastify, id)
    if (!quote) {
      return reply.code(404).send({ message: 'Quote not found' })
    }
    return quote
  })

  fastify.delete('/api/quotes/:id', deleteQuoteSchema, async function (request, reply) {
    const { id } = request.params
    const result = await deleteQuote(fastify, id)
    
    if (result.deletedCount === 0) {
      return reply.code(404).send({ message: 'Quote not found' })
    }
    
    return { message: 'Quote deleted successfully' }
  })
} 